var xEditor = Class.create();
xEditor.prototype = {
  dirname : null,
  imageNum: 0, /* type="file"のセレクタの数 */
  initialize: function(dirname){
      this.dirname = dirname;
  },
  loadFormState: function(){
      if ($('a_no_etime')){
          $('a_no_etime').onclick = function(e){
              if ($('a_no_etime').checked){
                  $('a_etime_date').value = "";
                  Element.hide('input_a_etime');
              } else {
                  Element.show('input_a_etime');
              }
          };
          $('a_no_etime').onclick();
      }
  },
  appendimage: function(selDOM){
      this.imageNum++;
      /// DIV
      var divEle = selDOM.getElementsByTagName("DIV");
      var divEle0 = divEle[0].cloneNode(true);
      var newName = divEle0.getElementsByTagName("INPUT")[0].name.match(/^(\w+)/) ? RegExp.$1 : 'imagefile';
      
      /// file element
      divEle0.getElementsByTagName("INPUT")[0].id = newName+this.imageNum;
      divEle0.getElementsByTagName("INPUT")[0].name = newName+"["+this.imageNum+"]";
      divEle0.getElementsByTagName("INPUT")[0].value = "";
      selDOM.insertBefore(divEle0, selDOM.lastChild);
  },
  
  openRealImage: function(img){
      var realimagepath = img.src.replace(/\/thumbnail/, '');
      window.open(realimagepath, 'win_image','width=510,height=400,status=no,scrollbars=yes,directories=no,menubar=no,resizable=yes,toolbar=no');
  },
  appendOnclick2Images: function(id,className){
      if (!$(id)) return null;
      $A($(id).getElementsByTagName("IMG")).each(function(img){
          if (img.src.match(/\/thumbnail\//)){
                  img.style.cursor="pointer";
                  img.onclick = (function(img){
                      return function(){
                          var realimagepath = img.src.replace(/\/thumbnail/, '');
                          xnews.openRealImage(img);
                      };
                  })(img);
              }
          });
  },
  changeArticleState: function(sel){
      return (function(selector){
          if (confirm('Change O.K ?')){ // 変更してよろしいですか？
              var a_id = selector.name.match(/\[(\d+)\]/) ? RegExp.$1 : 0;
              if (a_id==0) return null;
              var ajax = new Ajax.Request(
                  './index.php',{
                    method: 'post',
                    parameters: 'action_admin_json_news_changestatus=1&a_id='+a_id+'&a_status='+selector.value,
                    requestHeaders: ['Referer', location.href],
                    onSuccess: function(httpobj,json){
                        if (json && json.result && json.result==1){
                            selector.parentNode.firstChild.innerHTML = selector.value;
                            alert("Success"); // 状態を変更しました
                        } else {
                            alert("Failed"); // 状態の変更に失敗しました。
                            selector.value = parseInt(selector.parentNode.firstChild.innerHTML);
                        }
                        selector.parentNode.parentNode.className = (selector.value==0)?"odd":"even";
                    }
                  });
          } else {
              selector.value = parseInt(selector.parentNode.firstChild.innerHTML);
          }
      })(sel);
  }
}
